package com.zl.exception;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.NotSerializableException;
import java.sql.SQLException;

/**
 * @Author ljm
 * @Date 2021/11/24 14:52
 */
@Controller
public class GlobalException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        String path = httpServletRequest.getContextPath();
        String basePath = httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort() + path + "/";
        ModelAndView mv = new ModelAndView();
        if(e instanceof NullPointerException){
            System.out.println("空指针异常！");
            e.printStackTrace();
            mv.addObject("message","空指针异常！");
        }
        if(e instanceof ClassNotFoundException){
            System.out.println("指定的类找不到！");
            e.printStackTrace();
            mv.addObject("message","指定的类找不到");
        }
        if(e instanceof SQLException){
            System.out.println("SQL异常！");
            e.printStackTrace();
            mv.addObject("message","SQL异常！");
        }
        if(e instanceof NoSuchMethodException){
            System.out.println("方法不存在异常！");
            e.printStackTrace();
            mv.addObject("message","方法不存在异常！");
        }
        if(e instanceof NumberFormatException){
            System.out.println("字符串转换为数字异常！");
            e.printStackTrace();
            mv.addObject("message","字符串转换为数字异常！");
        }
        if(e instanceof IndexOutOfBoundsException){
            System.out.println("数组角标越界异常！");
            e.printStackTrace();
            mv.addObject("message","数组角标越界异常！");
        }
        if(e instanceof NegativeArraySizeException){
            System.out.println("数组负下标异常！");
            e.printStackTrace();
            mv.addObject("message","数组负下标异常！");
        }
        if(e instanceof FileNotFoundException){
            System.out.println("文件未找到异常！");
            e.printStackTrace();
            mv.addObject("message","文件未找到异常！");
        }
        if(e instanceof IllegalArgumentException ){
            System.out.println("方法传递参数错误！");
            e.printStackTrace();
            mv.addObject("message","方法传递参数错误！");
        }
        if(e instanceof IllegalAccessException){
            System.out.println("没有访问权限！");
            e.printStackTrace();
            mv.addObject("message","没有访问权限！");
        }
        if(e instanceof ClassCastException){
            System.out.println("数据类型转换异常！");
            e.printStackTrace();
            mv.addObject("message","数据类型转换异常！");
        }
        if(e instanceof NotSerializableException){
            System.out.println("未序列化异常！");
            e.printStackTrace();
            mv.addObject("message","未序列化异常！");
        }
        if(e instanceof InstantiationException){
            System.out.println("实例化异常!");
            e.printStackTrace();
            mv.addObject("message","实例化异常!");
        }
        if(e instanceof NoSuchMethodException){
            System.out.println("方法不存在异常!");
            e.printStackTrace();
            mv.addObject("message","方法不存在异常!");
        }
        if(e instanceof MaxUploadSizeExceededException){
            System.out.println("上传图片过大！");
            e.printStackTrace();
            mv.addObject("message","上传图片过大！");
        }
        if(e instanceof ArithmeticException){
            System.out.println("程序出现算数异常！");
            e.printStackTrace();
            mv.addObject("message","程出现算数异常！");
        }
        mv.setViewName("redirect:" + basePath + "error.jsp");
        //mv.setViewName("redirect:/error.jsp");
        return mv;
    }
}
